<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsBatchStock extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $goods_id = $this->request->post('goods_id');
            if(!empty($goods_id)){
                $where['gs.goods_id'] = ['in',$goods_id];
            }
            $batch_id = $this->request->post('batch_id');
            if(!empty($batch_id)){
                $where['gs.batch_id'] = ['in',$batch_id];
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_batch_stock')->alias('gs')
                    ->join('kuyou_goods g','g.id=gs.goods_id')
                    ->join('kuyou_goods_batch b','b.id=gs.batch_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->group('gs.goods_id,gs.stores_id,gs.batch_id')
                    ->field('sum(gs.stock) as stock,gs.batch_id,gs.goods_id,gs.stores_id')
                    ->order('gs.goods_id ASC')->select();
            }else{
                $list = Db::name('kuyou_goods_batch_stock')->alias('gs')
                    ->join('kuyou_goods g','g.id=gs.goods_id')
                    ->join('kuyou_goods_batch b','b.id=gs.batch_id')
                    ->where($where)
                    ->group('gs.goods_id,gs.stores_id,gs.batch_id')
                    ->field('sum(gs.stock) as stock,gs.batch_id,gs.goods_id,gs.stores_id')
                    ->order('gs.goods_id ASC')->select();
            }
            $row_count = Db::name('kuyou_goods_batch_stock')->alias('gs')
                ->join('kuyou_goods g','g.id=gs.goods_id')
                ->join('kuyou_goods_batch b','b.id=gs.batch_id')
                ->where($where)
                ->group('gs.goods_id,gs.stores_id,gs.batch_id')->count();

            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];

            $goods_id = isset($list[0]['goods_id']);
            $lists = [];$stock = 0;$stock_all = 0;
            foreach ($list as $list_k=>$list_v) {
                $stock_all += $list_v['stock'];
                if($goods_id!=$list_v['goods_id']){
                    $goods_id=$list_v['goods_id'];
                    $lists[] = ['store_name'=>'小计','stock'=>round($stock,$number_decimal)];
                    $stock = 0;
                }else{
                    $stock += $list_v['stock'];
                }
                $goods = Db::name('kuyou_goods')->where(['id'=>$list_v['goods_id']])->field('more_unit,unit_id,goods_code,goods_name,goods_barcode,goods_specification_id')->find();
                $list[$list_k]['goods_code'] = $goods['goods_code'];
                $list[$list_k]['goods_name'] = $goods['goods_name'];
                $list[$list_k]['goods_spec'] = $goods['goods_specification_id'];

                $unit = Db::name('kuyou_unit')->where(['id'=>$goods['unit_id']])->field('id,name')->find();
                $list[$list_k]['unit_name'] = $unit['name'];

                $max_unit = 1;
                $unit_arr = [];
                $more_unit_num = '';
                if($unit){
                    $unit_arr[] = ['name'=>$unit['name'],'advance_rate'=>1];
                    if($goods['more_unit']==1){
                        $more_unit = Db::name('kuyou_unit_conversion')->alias('uc')
                            ->join('kuyou_unit u','u.id=uc.vice_unit')
                            ->where(['uc.main_unit'=>$unit['id']])
                            ->order('uc.advance_rate','ASC')
                            ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                            ->select();
                        foreach ($more_unit as $unit_k=>$unit_v){
                            $unit_arr[] = ['name'=>$unit_v['name'],'advance_rate'=>$unit_v['advance_rate']];
                            if($unit_v['advance_rate']>$max_unit){
                                $max_unit = $unit_v['advance_rate'];
                            }
                        }
                        $unitconversion = '';
                        $unit_arr = (new Commons())->arraySequence($unit_arr,'advance_rate','SORT_DESC');
                        foreach ($unit_arr as $unit_arr_k=>$unit_arr_v){
                            if(!empty($unitconversion)){
                                $unitconversion .= '=';
                            }else{
                                $more_unit_num .= round(($list_v['stock']/$unit_arr_v['advance_rate']),$number_decimal).$unit_arr_v['name'];
                            }
                            $unitconversion .= round(($max_unit/$unit_arr_v['advance_rate']),$number_decimal).$unit_arr_v['name'];
                        }
                        $list[$list_k]['unitconversion'] = $unitconversion;
                        $list[$list_k]['more_unit_num'] = $more_unit_num;
                    }
                }
                if(!isset($list[$list_k]['unitconversion'])){
                    $list[$list_k]['unitconversion'] = '';
                    $list[$list_k]['more_unit_num'] = '';
                }

                $stores = Db::name('kuyou_stores')->where(['id'=>$list_v['stores_id']])->field('store_code,name')->find();
                $list[$list_k]['store_name'] = $stores['name'];

                $batch = Db::name('kuyou_goods_batch')->where(['id'=>$list_v['batch_id']])->field('batch_name,manufacture,guarantee,validity')->find();
                $list[$list_k]['batch_name'] = $batch['batch_name'];
                $time = ceil(($batch['validity']-time())/86400);
                $list[$list_k]['times'] = $time;
                $list[$list_k]['manufacture'] = date('Y-m-d',$batch['manufacture']);
                $list[$list_k]['guarantee'] = $batch['guarantee'];
                $list[$list_k]['validity'] = date('Y-m-d',$batch['validity']);

                $lists[] = $list[$list_k];
                if(($list_k+1)==sizeof($list)){
                    $goods_id=$list_v['goods_id'];
                    $lists[] = ['store_name'=>'小计','stock'=>round($stock,$number_decimal)];
                    $stock = 0;
                }
            }

            $result = array("count" => $row_count, "list" => $lists,'stock_all'=>round($stock_all,$number_decimal));

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
